package com.lyz.dataStructure.LeetCode.dongtaiguihua;

/*
leetcode 64题 最短路径和
 */
class Solution3 {
    public static int minPathSum(int[][] grid) {
        if(grid.length==0||grid[0].length==0){
            return 0;
        }
        int m = grid.length;
        int n =grid[0].length;
        int[] dp = new int[n];
        for(int i =0;i<m;i++){
            for(int j =0;j<n;j++){
                if(j==0){
                    dp[j] =dp[j];
                }else if(i==0) {
                    dp[j] =dp[j-1];
                }else {
                    dp[j] = Math.min(dp[j-1],dp[j]);
                }

                dp[j] = dp[j]+grid[i][j];
            }
        }
       return dp[n-1];
    }

    public static void main(String[] args) {
        int[][] nums ={
                {1,3,6},
                {4,5,2},
                {4,2,1}
        };
        System.out.println(minPathSum(nums));
    }

}
